import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/user/Users'
import Rights from '../components/rights/Rights'
import Roles from '../components/rights/Roles'
import Cate from '../components/goods/Cate'
import Params from '../components/goods/Params'
import List from '../components/goods/List'
import addGood from '../components/goods/AddGood'
import Order from '../components/order/Order'
import Report from '../components/echarts/Report'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  // 默认的跳转到登录界面，而不是根目录
  redirect: '/login'
},
{
  path: '/login',
  component: Login
},
{
  path: '/home',
  component: Home,
  // 进入界面之后显示欢迎界面
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    component: Welcome
  }, {
    path: '/users',
    component: Users
  }, {
    path: '/roles',
    component: Roles
  }, {
    path: '/rights',
    component: Rights
  }, {
    path: '/categories',
    component: Cate
  }, {
    path: '/params',
    component: Params
  }, {
    path: '/goods',
    component: List
  }, {
    path: '/goods/addgood',
    component: addGood
  }, {
    path: '/orders',
    component: Order
  }, {
    path: '/reports',
    component: Report
  }]
}
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // token的作用，可以在一个网站登陆后的页面内任意跳转，作为一个判断
  if (to.path === '/login') return next()
  else {
    const token = sessionStorage.getItem('token')
    if (!token) {
      return next('/login')
    }
    next()
  }
})
export default router
